package com.plian.system.controller.ms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.constant.sys.FormStatusConstant;
import com.plian.system.entity.ms.DailyThing;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.ms.IDailyThingService;
import com.plian.system.vo.ms.DailyThingVO;
import com.plian.system.wrapper.ms.DailyThingWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @author huzb
 * @create 2020-11-09 10:50
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/dailything")
public class DailyThingController {

    private IDailyThingService iDailyThingService;

    private DailyThingWrapper dailyThingWrapper;

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @LogOperation("日常事物报告-提交")
    public R submit(@Valid @RequestBody DailyThing dailyThing) {
        if (Optional.ofNullable(dailyThing.getStatus()).isPresent() &&
                dailyThing.getStatus() != FormStatusConstant.STASH){
            if (!iDailyThingService.checkIsExisted(dailyThing.getId(), dailyThing.getCode())){
                return new R(CustomCode.builder().message("单据编码已存在").code(1999).success(false).build());
            }
        }
        return iDailyThingService.submit(dailyThing) ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @LogOperation("日常事物报告-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return iDailyThingService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    /**
     * 详情
     */
    @PostMapping("/detail")
    public R<DailyThingVO> detail(@Valid @RequestBody DailyThing dailyThing) {
        DailyThing detail = iDailyThingService.getOne(Condition.getQueryWrapper(dailyThing));
        return R.data(dailyThingWrapper.entityToVO(detail));
    }

    /**
     * 分页
     */
    @PostMapping("/list")
    public ListResult<DailyThingVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<DailyThing> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new DailyThing()), entityList
        );
        TokenSearch<DailyThing> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);
        IPage<DailyThing> pages = iDailyThingService.page(Condition.getPage(query), queryWrapper);
        List<DailyThingVO> dailyThingVOList = dailyThingWrapper.entityToVO(pages.getRecords());
        ListResult<DailyThingVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(dailyThingVOList);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }

}
